<?php

namespace App\Http\Controllers\Car;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Car\CarCost;
use App\Models\Car\ExternalTransfer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ExternalTransferController extends Controller
{
    use ResponseData;
    public function index(ExternalTransfer $model,Request $request) {
        $size = $request->input('size',10);
        if($request->filled('department')){
            $model = $model->where('department',$request->department);
        }
        if($request->filled('department_id')){
            $model = $model->where('department_id',$request->department_id);
        }
        if($request->filled('plate_number')){
            $model = $model->where('plate_number','like','%'.$request->plate_number.'%');
        }
        if($request->filled('order_sn')){
            $model = $model->where('order_sn','like','%'.$request->order_sn.'%');
        }
        $data = $model->paginate($size);
        return $this->success($data);
    }
    // 统计金额
    public function getTotal(ExternalTransfer $model,Request $request){
        $model = $model->query()->selectRaw("SUM(transfer_price) as transfer_count,SUM(lease_price) as lease_count");
        if($request->filled('department')){
            $model = $model->where('department',$request->department);
        }
        if($request->filled('department_id')){
            $model = $model->where('department_id',$request->department_id);
        }
        $data = $model->first();
        return $this->success($data);
    }
    public function store(ExternalTransfer $model,Request $request){
        $data = $request->all();
        $data['order_sn'] = $this->getSn();
        $model->fill($data);
        $model->save();
        return $this->success('保存成功');
    }
    //设置订单号
    private function getSn(){
        $car = ExternalTransfer::query()
            ->select('id')
            ->where('id','>',0)
            ->orderByDesc('id')
            ->first();
        if($car){
            $num = $car->id+1;
        }else{
            $num = 1;
        }
        return 'wd'.date('Ymd',time()).$num;
    }
    public function edit(ExternalTransfer $model,Request $request){
        if($request->filled('order_sn')){
            $data = $model->where('order_sn',$request->order_sn)->first();
        } else {
            $data = $model->find($request->id);
        }
        return $this->success($data);
    }
    public function update(ExternalTransfer $model,Request $request){
        $data = $model->find($request->id);
        if($request->filled('send_employer_id')){
            if($data->send_employer_id!=0){
                return $this->failed('订单已被接单',201);
            }
        }
        if($request->filled('return_employer_id')){
            if($data->return_employer_id!=0){
                return $this->failed('订单已被接单',201);
            }
        }
        if($request->filled('clieaner_id')){
            if($data->clieaner_id!=0){
                return $this->failed('订单已被接单',201);
            }
        }
        $data->update($request->all());
        return $this->success('修改成功');
    }
    public function destory(ExternalTransfer $model,Request $request){
        $data = $model->find($request->id);
        $data->delete();
    }
}
